package com.iqiyi.pps.epg.core.service.log;

import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.log.LogOperatorDao;
import com.iqiyi.pps.epg.core.model.auth.SysUser;
import com.iqiyi.pps.epg.core.model.log.LogOperator;
import com.iqiyi.pps.epg.core.utils.SpringSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Qiyi
 * Date: 12-6-6
 * Time: 下午7:12
 * To change this template use File | Settings | File Templates.
 */

@Component
@Transactional
public class LogOperatorManager {
    @Autowired
    private LogOperatorDao logOperatorDao;

    public void saveLogOperator(LogOperator logOperator) {
        logOperatorDao.save(logOperator);
    }

    @Transactional(readOnly = true)
    public Page<LogOperator> searchLogOperator(final Page<LogOperator> page, final List<PropertyFilter> filters) {
        Iterator<GrantedAuthority> iterator = SpringSecurityUtils.getCurrentUser().getAuthorities().iterator();
        boolean all = false;
        while (iterator.hasNext()) {
            GrantedAuthority grantedAuthority = iterator.next();
            if (grantedAuthority.getAuthority().equals("AUTH_ALL"))
                all = true;
        }
        if (!all) {
            PropertyFilter propertyFilter = new PropertyFilter("EQS_operator", ((SysUser) SpringSecurityUtils.getCurrentUser()).getName());
            filters.add(propertyFilter);
        }
        return logOperatorDao.findPage(page, filters);
    }

    public Object getObject(Class c, Long id) {
        return logOperatorDao.getByEntityClassAndId(c, id);
    }

}
